package com.fhz.kotlinmvvm

import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Message
import android.view.View
import android.view.animation.BounceInterpolator
import android.view.animation.LinearInterpolator
import com.fhz.kotlinmvvm.databinding.ActivitySplashBinding

/**
 * 欢迎页
 */
class SplashActivity : AppCompatActivity() {


    val handler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)

        handler.sendEmptyMessageDelayed(0, 3000)

        val downTime = object : CountDownTimer(3000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                binding.splashTime.text = (millisUntilFinished / 1000).toString() + "秒"
            }

            override fun onFinish() {
                finish()
                MainActivity.launch(this@SplashActivity)
                overridePendingTransition(R.anim.fade_in,R.anim.fade_out)
            }
        }
        downTime.start()

        binding.splashBtn.apply {
            setOnClickListener {
                downTime.cancel()
//                MainActivity.launch(this@SplashActivity)
//                finish()
            }
        }


        binding.indexCircle.pivotX = 0f
        binding.indexCircle.pivotY = binding.indexCircle.height.toFloat()

        val animator = ObjectAnimator.ofFloat(binding.indexCircle, "rotation", -45f, 0f)
        animator.interpolator = BounceInterpolator()
        animator.duration = 5000
        animator.start()

        val tvAnimator = ObjectAnimator.ofFloat(binding.indexWelcome, "alpha", 0f, 1f)
        tvAnimator.duration = 3000
        tvAnimator.start()

        val ImAnimator = ObjectAnimator.ofFloat(binding.indexArrowsRed, "translationX", -200f, 0f)
        ImAnimator.duration = 3000
        ImAnimator.start()


    }

}